﻿Public Class UpdateDeleteShopOutletAdmin
    Inherits System.Web.UI.Page
    Public dbmanager As DBManager = New DBManager

    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            ' Response.Redirect("~/Login.aspx")
        End If
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        If Not IsPostBack Then
            Try
                If (Request.QueryString("id") IsNot Nothing) Then
                    Dim fullID = Request.QueryString("id")

                    Dim type = fullID.Substring(0, 1)


                    Select Case type
                        Case "b"
                            Dim BusinessID = fullID.Substring(1, fullID.Length - 1)
                            'get business details by businessID
                            Dim businessData = dbmanager.getBusinessDetailsBID(BusinessID)
                            tbxBusinessName.Text = businessData(0)
                            ddlCategory.SelectedItem.Text = businessData(1)
                            tbxShopURL.Text = businessData(2)
                            tbxShopDetails.Text = businessData(3)
                            BusinessDetailPanel.Visible = True
                            OutletDetailsPanel.Visible = False
                        Case "o"

                            Dim OutletID = fullID.Substring(1, fullID.Length - 1)
                            'get the outlet details by outletID
                            Dim OutletData = dbmanager.getOutletDetails(OutletID)
                            tbxShopOutletName.Text = OutletData(0)
                            tbxOutletAddress.Text = OutletData(1)
                            tbxOpeningHours.Text = OutletData(2)
                            tbxRemarks.Text = OutletData(3)
                            tbxShopOutletTel.Text = OutletData(4)
                            BusinessDetailPanel.Visible = False
                            OutletDetailsPanel.Visible = True

                    End Select

                Else
                    Response.Redirect("home.aspx")
                End If
            Catch ex As Exception
            End Try
        End If

    End Sub


    Protected Sub BtnBusinessDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnBusinessDelete.Click
        ' Msg box telling the user that if they delete the business , they will have to delete the outlets first

    End Sub


    Protected Sub BtnBusinessUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnBusinessUpdate.Click

        If (Request.QueryString("id") IsNot Nothing) Then
            Dim fullID = Request.QueryString("id")
            Dim type = fullID.Substring(0, 1)
            Select Case type
                Case "b"
                    Dim datecreated As Date
                    datecreated = Today.Date
                    Dim BusinessID = fullID.Substring(1, fullID.Length - 1)
                    dbmanager.updateBusinessDetails(BusinessID, tbxBusinessName.Text, ddlCategory.SelectedItem.Text, tbxShopURL.Text, tbxShopDetails.Text, datecreated)
                    'update business details 
                    MsgBox("Successfully update business", MsgBoxStyle.OkOnly, "Success")

            End Select
        End If
    End Sub

    Protected Sub BtnBusinessUpdate0_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnBusinessUpdate0.Click

        If (Request.QueryString("id") IsNot Nothing) Then
            Dim fullID = Request.QueryString("id")
            Dim type = fullID.Substring(0, 1)
            Select Case type
                Case "o"
                    Dim datecreated As Date
                    datecreated = Today.Date
                    Dim outletID = fullID.Substring(1, fullID.Length - 1)
                    dbmanager.updateOutletDetails(outletID, tbxShopOutletName.Text, tbxOutletAddress.Text, tbxOpeningHours.Text, tbxRemarks.Text, 2, tbxShopOutletTel.Text, datecreated)
            End Select
        End If
    End Sub
End Class